// pages/entering/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    columns: [],
    columnsArr: [],
    typeId: '',
    typeIntro:'',
    typeDemand: '',
    typeName: '',
    typeTel: '',
    _typeName:'',
    showText: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  changeType () {
    var that = this
    util.http({
      url: 'api/miniwechat/center/category',
      data: {
        type: "Settled"
      }
    }).then(res => {
      // console.log(res)
      let arr = []
      res.forEach((item, index)=> {
        arr.push(item.name)
      })
      
      that.setData({
        columns: arr,
        columnsArr: res
      })
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
    this.setData({
      show: true,
      showText: false
    })
  },

  onClose () {
    this.setData({
      show: false,
      showText: true
    })
  },
 
  onConfirm(event) {
    let index = event.detail.index;
    let _array = this.data.columnsArr
    let typeId = _array[index].id
    let _typeName = _array[index].name
    this.setData({
      typeId: typeId,
      _typeName: _typeName
    })
    
    this.onClose()
  },

  onInput (e) {
    if (e.currentTarget.dataset.type =="intro"){
      let typeIntro = e.detail.value;
      this.setData({
        typeIntro: typeIntro
      })
    }
    if (e.currentTarget.dataset.type == "demand") {
      let typeDemand = e.detail.value
      this.setData({
        typeDemand: typeDemand
      })
    }
    if (e.currentTarget.dataset.type == "name") {
      let typeName = e.detail.value
      this.setData({
        typeName: typeName
      })
    }
    if (e.currentTarget.dataset.type == "tel") {
      let typeTel = e.detail.value
      this.setData({
        typeTel: typeTel
      })
    }
  },
  onSumbit () {
    if (!this.checkInput(this.data._typeName, "请选择入驻类型")) return false
    if (!this.checkInput(this.data.typeIntro, "请输入您的情况")) return false
    if (!this.checkInput(this.data.typeDemand, "请输入您的需求")) return false
    if (!this.checkInput(this.data.typeName, "请输入您的名字")) return false
    if (!this.checkInput(this.data.typeTel, "请输入您的号码")) return false
    let reg = /^1[34578]\d{9}$/;
    if (!reg.test(this.data.typeTel)) {
      wx.showToast({
        title: "请输入正确的电话号码",
        icon: 'none',
        duration: 2000
      })
      return false
    }
    var that = this
    util.http({
      url: 'api/miniwechat/center/settled',
      method: 'POST',
      headers: {
        token: util.isLogin().token
      },
      data: {
        type_id: that.data.typeId,
        introduce: that.data.typeIntro,
        needs: that.data.typeDemand,
        phone: that.data.typeName,
        contacts: that.data.typeTel
      }
    }).then(res => {
      wx.showToast({
        title: '发送成功',
        icon: 'none',
        duration: 1000
      })
      setTimeout(() =>{
        wx.navigateBack({
          delta: 1
        })
      },1000)
    }).catch(ex => {
      wx.showToast({
        title: String(ex),
        icon: 'none',
        duration: 2000
      })
    })
  },

  checkInput (data, msg) {
    if (data == null || data == '') {
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 2000
      })
      return false;
    } else {
      return true;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})